package com.example.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.Result;
import com.example.entry.OrderGrabbing;
import com.example.service.IOrderGrabbingService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/*
 * 骑手抢单前台控制器
 * */
@RequestMapping("/web/order-grabbing")
@RestController
public class OrderGrabbingWebController {

    @Resource
    private IOrderGrabbingService orderGrabbingService;

    /*
    * 未接单
    * */
    @GetMapping("/received")
    public Result unacceptedOrders(@RequestParam Integer status,
                                   @RequestParam(defaultValue = "") Integer riderId){
        QueryWrapper<OrderGrabbing> wrapper = new QueryWrapper<>();
        wrapper.eq("status", status);
        // 如果骑手id不为空，则根据骑手id进行查询
        if(Objects.nonNull(riderId)){
            wrapper.eq("rider_id", riderId);
        }
        List<OrderGrabbing> list = orderGrabbingService.list(wrapper);
        return Result.success("success", list);
    }

    /*
    * 骑手接单
    * */
    @PostMapping("/rider-connect")
    public Result riderConnectOrder(@RequestBody OrderGrabbing orderGrabbing){
        orderGrabbingService.riderConnect(orderGrabbing);
        return Result.success("抢单成功");
    }

    /*
    * 订单送达
    * */
    @PostMapping("/order-over")
    public Result orderOver(@RequestBody OrderGrabbing orderGrabbing){
        orderGrabbingService.orderComplete(orderGrabbing);
        return Result.success();
    }

    /*
    * 订单删除
    * */
    @GetMapping("/del/id={id}")
    public Result delOrderGrabbing(@PathVariable Integer id){
        orderGrabbingService.removeById(id);
        return Result.success("删除成功");
    }
}
